/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package simulation;

import base.node.WSNode;
import base.server.Broker;
import gui.MessageFrame;
import utilities.DrawingBean;
import base.node.NodeArchBean;
import java.util.Vector;
import base.node.NodeInfo;
import base.node.PowerPolicy;
import java.awt.Point;
import networking.Client;
import networking.Server;
import networking.SocketMessageFactory;

/**
 * Manages the simulation process
 * @author Arvanitis Ioannis
 */
public class SimulationManager {

    private static Broker broker;
    private static Vector<DrawingBean> nodes;
    private static int portOffset;
    private static WSNode[] node;
    private static SocketMessageFactory smf;
    private static Client client = null;

    /**
     * Gets all the nodes in simulation
     * @return all the nodes in simulation
     */
    public static WSNode[] getNode() {
        return node;
    }

    /**
     * Sets all the nodes in simulation
     * @param node All the nodes in simulation
     */
    public static void setNode(WSNode[] node) {
        SimulationManager.node = node;
    }

    /**
     * Gets the indexed node in simulation
     * @param index The index of the node (id-1)
     * @return the indexed node in simulation
     */
    public static WSNode getNode(int index) {
        return node[index];
    }

    /**
     * Starts the simulation
     */
    public static void start() {

        nodes = GUIOptionManager.getNodes();
        portOffset = GUIOptionManager.getCommOffset();
        node = new WSNode[nodes.size()];


        //setup the network
        if (GUIOptionManager.getIp().equals("127.0.0.1")) {
            //Setup the broker
            broker = new Broker(portOffset, nodes.size());
            broker.run();
        } else {
            Server server = new Server(30000);
            smf = new SocketMessageFactory();
            smf.setMsgType("CONNECTION_INFO");
            smf.setPort(portOffset);
            server.sendMessage(smf);
            client = new Client(GUIOptionManager.getIp(), portOffset);
            smf = new SocketMessageFactory();
            smf.setMsgType("BROKER_START");
            smf.setTotalNodes(nodes.size());
            client.sendMessage(smf);
        }

        //Setup the nodes
        for (int i = 0; i < nodes.size(); i++) {
            NodeArchBean nab = GUIOptionManager.getNodeArchPerCategory().get(Integer.parseInt(nodes.get(i).getInfo()) - 1);
            NodeInfo ni = new NodeInfo(i + 1, nab.getEnergy_mWh(), new Point(nodes.get(i).getPoint()), nab.getDaemonIntervalInSec(), nab.getAntennaRadPow(), nab.getAntennaThres());
            PowerPolicy pp = new PowerPolicy(nab.getEnConsMove(), nab.getEnConsIdle(), nab.getEnConsTransmit(), nab.getEnConsReceive(), nab.getEnConsProcess());
            node[i] = new WSNode(ni, pp, portOffset);
        }
        GUIOptionManager.setWsnodes(getNode().clone());

        if (!GUIOptionManager.getIp().equals("127.0.0.1")) {
            client.sendMessage(new GUIOptionTransformer());
        }

        for (int i = 0; i < nodes.size(); i++) {
            node[i].start();
        }

    }

    /**
     * Stops the simulation
     */
    public static void stop() {
        if (!GUIOptionManager.getIp().equals("127.0.0.1")) {
            smf = new SocketMessageFactory();
            smf.setMsgType("BROKER_STOP");
            client.sendMessage(smf);
        } else {
            broker.interrupt();
        }
    }

    public static void simulate() {

        new MessageFrame();


    }
}
